<?php

/**
 * @package Nina
 * @author John Main <john@thedaddy.org>, Andrew Rose <rose.andrew@gmail.com>
 */
class Nina_Db
{
	protected $reg;
	protected $driver;

	public function init(Nina_Registry $reg) {
		$this->reg = $reg;

		if($reg->exists('driver')) {
			$class = 'Nina_Db_Driver_'.$reg->get('driver');
			if(Nina::classExists($class)) {
				$this->driver = new $class;
				$this->driver->init($this->reg);
			}
			else
				throw new Nina_Exception_Db("Unknown driver: ".$reg->get('driver'));
		}
		else
			throw new Nina_Exception_Db("No driver specified.");

		return TRUE;
	}
	
	public function getName() {
		return $this->reg->get('database');
	}

	public function connect() {
		return $this->driver->connect();
	}

	public function database($database) {
		return $this->driver->database($database);
	}

	public function begin() {
		return $this->driver->query("BEGIN");
	}

	public function commit() {
		return $this->driver->query("COMMIT");
	}

	public function rollback() {
		return $this->driver->query("ROLLBACK");
	}

	public function getFields($table) {
		return $this->driver->getFields($table);
	}

	public function getTables() {
		return $this->driver->getTables();
	}

	public function query($query) {
		return $this->driver->query($query);
	}
	
	public function escape($data) {
		return $this->driver->escape($data);
	}
	
	public function insertId() {
		return $this->driver->insertId();
	}
	
	public function prepare($sql) {
		return $this->driver->prepare($sql);
	}
	
	public function lastError() {
		return $this->driver->lastError();
	}

	public function close() {
		return $this->driver->close();
	}

	public function ping() {
		return $this->driver->ping();
	}
}

